package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.bo.CourseBo;
import com.one.dto.PaginationDate;
import com.one.dto.Result;
import com.one.dto.ReturnDate;
import com.one.entity.CourseView;
import com.one.entity.SysCollegeEntity;
import com.one.entity.SysCourseEntity;
import com.one.entity.SysUserEntity;
import com.one.enums.IsDeletedEnum;
import com.one.enums.SexEnum;
import com.one.mapper.CourseMapper;
import com.one.service.SysCourseService;
import com.one.utils.DataCheckUtils;
import com.one.utils.DateUtils;
import com.one.utils.PaginationUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SysCourseServiceImpl extends ServiceImpl<CourseMapper, SysCourseEntity> implements SysCourseService {

    private final CourseMapper courseMapper;

    public SysCourseServiceImpl(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @Override
    public Result addCourse(SysCourseEntity  sysCourseEntity) {
        sysCourseEntity.setIsDeleted("0");
        sysCourseEntity.setCreateBy("1");
        sysCourseEntity.setCreateTime(DateUtils.getCurrentTime());
        int row = courseMapper.insert(sysCourseEntity);
        if (row == 0) {
            return Result.error().message("添加课程失败");
        }
        return Result.ok();
    }

    @Override
    public Result selectCourse(CourseBo courseBo) {
        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(courseBo.getNumber(), courseBo.getPage()), "分页参数错误!");
        int page = courseBo.getPage();
        courseBo.setPage(PaginationUtils.getPageIndex(courseBo.getPage(), courseBo.getNumber()));
        List<Object> result= courseMapper.selectCourse(courseBo);
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) {
            return Result.error().message("查询结果为空");
        }
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(courseBo.getNumber(), page, sum);
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate)){
            return Result.error().message("分页参数错误!");
        }
        return Result.ok().data(new ReturnDate(paginationDate,(List<CourseView>) result.get(0) ));

    }

    @Override
    public Result deleteCourse(SysCourseEntity sysCourseEntity) {
        DataCheckUtils.isNotBlank(sysCourseEntity.getCourseid(), "课程id不能为空!");
        QueryWrapper<SysCourseEntity> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(SysCourseEntity::getCourseid, sysCourseEntity.getCourseid()).eq(SysCourseEntity::getIsDeleted, IsDeletedEnum.NO.getKey());
        SysCourseEntity course = courseMapper.selectOne(sectionQueryWrapper);
        //查询结果校验
        if (course == null) {
            return Result.error().message("账号不存在！");
        }
        sysCourseEntity.setIsDeleted(IsDeletedEnum.YES.getKey());
        UpdateWrapper<SysCourseEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysCourseEntity::getCourseid, sysCourseEntity.getCourseid()).eq(SysCourseEntity::getIsDeleted,IsDeletedEnum.NO.getKey());
        int row = courseMapper.update(sysCourseEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除用户失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateCourse(SysCourseEntity sysCourseEntity) {
        sysCourseEntity.setUpdateBy(DateUtils.getCurrentTime());
        UpdateWrapper<SysCourseEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysCourseEntity::getCourseid, sysCourseEntity.getCourseid());
        int row = courseMapper.update(sysCourseEntity,updateWrapper);
        if (row == 0) {
            return Result.error().message("添加课程失败");
        }
        return Result.ok();
    }
}
